/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace DaveSexton.DocProject
{
	public sealed class VSEnvironmentBuildTrace : BuildTrace
	{
		#region Private / Protected
		private readonly string projectName;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="VSEnvironmentBuildTrace" /> class.
		/// </summary>
		public VSEnvironmentBuildTrace(string projectName)
		{
			this.projectName = projectName;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="VSEnvironmentBuildTrace" /> class.
		/// </summary>
		public VSEnvironmentBuildTrace()
		{
		}
		#endregion

		#region Methods
		public override void TraceLine()
		{
			VSEnvironmentHost.TraceLine();
		}

		public override void TraceLine(string message)
		{
			VSEnvironmentHost.TraceLine(message);
		}

		public override void TraceLine(string format, params object[] args)
		{
			VSEnvironmentHost.TraceLine(format, args);
		}

		public override void Trace(string message)
		{
			VSEnvironmentHost.Trace(message);
		}

		public override void Trace(string format, params object[] args)
		{
			VSEnvironmentHost.Trace(format, args);
		}

		public override void TracePath(string path)
		{
			VSEnvironmentHost.TracePath(path);
		}

		public override void TracePathLine(string path)
		{
			VSEnvironmentHost.TracePathLine(path);
		}

		public override void TraceEvent(System.Diagnostics.TraceEventType type, string category, string message, string file, int line, string userMessage)
		{
			switch (type)
			{
				case System.Diagnostics.TraceEventType.Warning:
					VSEnvironmentHost.BuildOutput.OutputTaskItemString(
						(string.IsNullOrEmpty(userMessage)) ? " " : userMessage,	// an empty string prevents the item from being added to the list
						EnvDTE.vsTaskPriority.vsTaskPriorityMedium,
						projectName + ((string.IsNullOrEmpty(category)) ? string.Empty : ":" + category),
						EnvDTE.vsTaskIcon.vsTaskIconSquiggle, file, line,
						message, false);
					break;
				case System.Diagnostics.TraceEventType.Error:
					VSEnvironmentHost.BuildOutput.OutputTaskItemString(
						(string.IsNullOrEmpty(userMessage)) ? " " : userMessage,	// an empty string prevents the item from being added to the list
						EnvDTE.vsTaskPriority.vsTaskPriorityHigh,
						projectName + ((string.IsNullOrEmpty(category)) ? string.Empty : ":" + category),
						EnvDTE.vsTaskIcon.vsTaskIconCompile, file, line,
						message, false);
					break;
				default:
					// Through testing it appears that there is no way to output informational messages.  When vsTaskPriorityHigh is used 
					// the item is added to the list as an error (unless the task icon is "vsTaskIconUser", in which case it's added as a warning).
					// For all other combinations of task priority and task icon values the item is always added as a warning.
					// The settings used below haven't been tested in VS 2008 so the most intuitive combination is being used with hope that it works.
					VSEnvironmentHost.BuildOutput.OutputTaskItemString(
						(string.IsNullOrEmpty(userMessage)) ? " " : userMessage,	// an empty string prevents the item from being added to the list
						EnvDTE.vsTaskPriority.vsTaskPriorityLow,
						projectName + ((string.IsNullOrEmpty(category)) ? string.Empty : ":" + category),
						EnvDTE.vsTaskIcon.vsTaskIconComment, file, line,
						message, false);
					break;
			}
		}

		public override void FlushEvents()
		{
			VSEnvironmentHost.BuildOutput.ForceItemsToTaskList();
		}

		public override void SetProgress(string text, int completed, int total)
		{
			VSEnvironmentHost.SetProgress(text, completed, total);
		}

		public override void SetStatus(string text)
		{
			VSEnvironmentHost.Status = text;
		}
		#endregion
	}
}
